<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='用户编辑',active='user'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <!-- 引入Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <!-- 引入jQuery库 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <!-- 引入Bootstrap JS -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<!-- 模态框 -->
<div class="modal-header">
    <h5 class="modal-title" id="editUserModalLabel">编辑商户信息</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <form  id="editForm"  th:object="${bususer}">
        <div class="form-group">
            <input type="hidden"  id="id" name="id" th:field="*{bus_user_id}" required>
        </div>
        <div class="form-group">
            <label for="name">名称：</label>
            <input type="text" class="form-control" id="name" name="username" th:field="*{bus_user_name}" required>
        </div>
        <div class="form-group">
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="text" class="form-control" id="password" name="password" th:field="*{bus_user_password}"  >
            </div>
        </div>
        <div class="form-group">
            <label for="tel">电话：</label>
            <input type="text" class="form-control" id="tel" name="tel" th:field="*{bus_user_tel}" required>
        </div>
        <div class="form-group">
            <label for="desc">评价：</label>
            <input type="text" class="form-control" id="desc" name="desc" th:field="*{bus_user_desc}"  required>
        </div>
        <div class="form-group">
            <label for="address">地址：</label>
            <input type="text" class="form-control" id="address" name="address" th:field="*{bus_user_address}"  required>
        </div>
        <div class="form-group">
            <label for="averagepriece">均价：</label>
            <input type="number" class="form-control" id="averagepriece" name="averagepreice" th:field="*{bus_user_averagepriece}" required >
        </div>
<!--        <div class="form-group">-->
<!--            <label for="averageprice">图片：</label>-->
<!--            <input type="file" id="uploadInput" accept="image/*">-->
<!--        </div>-->
        <button type="submit" class="btn btn-primary" id="saveBtn" >保存</button>
    </form>
</div>
<script>

    // 假设用户点击保存按钮时触发保存操作
    // document.getElementById('saveBtn').addEventListener('click', function(event) {
    //     event.preventDefault(); // 阻止表单默认提交行为
    //
    //     var user ={
    //         bus_user_id:parseInt($("#bus_user_id").val()),
    //         bus_user_name:$("#bus_user_name").val(),
    //         bus_user_password:$("#bus_user_password").val(),
    //         bus_user_tel:$("#bus_user_tel").val(),
    //         bus_user_desc:$("#bus_user_desc").val(),
    //         bus_user_address:$("#bus_user_address").val(),
    //         bus_user_averageprice:$("#bus_user_averageprice").val(),
    //     }
    //     var fileInput = document.getElementById("uploadInput");
    //     var file = fileInput.files[0];
    //     // 发送保存请求
    //     $.ajax({
    //         url: "/admin/bususer/saveUser",
    //         type: "GET",
    //         data: user,file,
    //         contentType: "application/json",
    //         success: function(response) {
    //             console.log("User information saved successfully.");
    //             // 进行保存成功后的操作，例如刷新页面或显示成功提示
    //             $("#editModal").modal("hide");
    //             location.reload();
    //         },
    //         error: function(xhr, status, error) {
    //             console.error("Failed to save user information:", error);
    //             // 处理保存失败的情况，例如显示错误提示
    //         }
    //     });
    // });
    $(document).ready(function() {
        // 处理保存按钮的点击事件
        $("#saveBtn").click(function(e) {
            e.preventDefault(); // 阻止默认的表单提交行为

            var user ={
                bus_user_id:parseInt($("#id").val()),
                bus_user_name:$("#name").val(),
                bus_user_password:$("#password").val(),
                bus_user_tel:$("#tel").val(),
                bus_user_desc:$("#desc").val(),
                bus_user_address:$("#address").val(),
                bus_user_averagepriece:$("#averagepriece").val()
            }

            console.log(user)
            // var fileInput = document.getElementById("uploadInput");
            // var file = fileInput.files[0];


            // 发送AJAX请求保存表单数据
            $.ajax({
                url: "/admin/bususer/saveUser",
                type: "GET",
                data: user,
                contentType: "application/json",
                success: function(response) {
                    // 处理成功响应
                    console.log("表单数据保存成功");
                    $("#editModal").modal("hide");
                    location.reload();
                },
                error: function(xhr, status, error) {
                    // 处理错误响应
                    console.error("保存表单数据出错：", error);
                    // 可以显示错误消息或执行其他错误处理操作
                }
            });
        });
    });

</script>
</html>




